package Testing;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class tableArray {

	public static void main(String[] args) {
		tableArray gt = new tableArray();
		ArrayList<Object> retObjArr = gt.getTableArray("C:\\Users\\mkasim\\Desktop\\Login.xls", "Sheet1", "Login");
		String [] a= (String[]) retObjArr .get(0);
/*		System.out.println(a[0]);
*/	}

	public ArrayList<Object> getTableArray(String xlFilePath, String sheetName,
			String tableName) {
		String[][] Data = null;
		/*String[][] tabArray1 = null;*/
		String tableStartName = tableName + "Begin";
		List<Map<String,String>> HeadData=null;
		String tableEndName = tableName + "End";
		try {
			Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
			Sheet sheet = workbook.getSheet(sheetName);
			int startRow, startCol, endRow, endCol, ci, cj;
			Cell tableStart = sheet.findCell(tableStartName);
			startRow = tableStart.getRow();
			startCol = tableStart.getColumn();
			/*
			 * Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1,
			 * 100, 64000, false);
			 */
			Cell tableEnd = sheet.findCell(tableEndName);
			endRow = tableEnd.getRow();
			endCol = tableEnd.getColumn();
			
			  /*System.out.println("startRow=" + startRow + ", endRow=" + endRow
			  + ", " + "startCol=" + startCol + ", endCol=" + endCol);
			 

			System.out.println("Rows: " + (endRow - startRow - 2) + "Columns: "
					+ (endCol - startCol + 1));
*/
			int Rows = endRow - startRow - 2;
			int Columns = endCol - startCol + 1;

			Data = new String[endRow - startRow - 2][endCol - startCol + 1];

			String[] Headers = new String[Columns];

			ci = 0;
			for (int i = 0; i < 1; i++, ci++) {
				cj = 0;
				for (int j = 0; j < Columns; j++, cj++) {
					Headers[j] = (sheet.getCell(j + startCol, i + startRow + 1)
							.getContents());
			/*		System.out.println(Headers[j]);*/
				}
			}

			
			ci = 0;
			for (int i = 0; i < Rows; i++, ci++) {
				cj = 0;
				for (int j = 0; j < Columns; j++, cj++) {
					Data[ci][cj] = sheet.getCell(j + startCol,
							i + startRow + 2).getContents();
				}
			}

			/* tabArray1 = new String[endCol + 1][endRow - startRow - 1]; */

/*			for (int j = 0; j < Rows; j++) {
				for (int i = 0; i < Columns; i++) {
					System.out.println("ROW" + j + "COL" + i + Data[j][i]);
				}
			}
*/			Arrlist ar=new Arrlist();
			HeadData = ar.resultset(Headers, Data);
			/*System.out.println(HeadData.get(0).get("UserName"));*/
			/*System.out.println(HeadData.toString());*/
		} catch (Exception e) {
			System.out.println("error in getTableArray()");
			e.printStackTrace();
		}

		/*String [] Headers = HeadData.get(0).keySet().toString().replace("[","").replace("]","").split(",");*/
		String [] Headers = HeadData.get(0).keySet().toString().split(",");
		
		/*System.out.println("retObjArr.get(0).keySet()"+retObjArr.get(0).keySet());*/
		for(int i=0;i<Headers.length;i++)
		{
			Headers[i]=Headers[i].trim();
		}
		Headers[0] =Headers[0].substring(1, Headers[0].length());
		Headers[Headers.length-1] =Headers[Headers.length-1].substring(0, Headers[Headers.length-1].length()-1);
/*		for (int i = 0; i < Headers.length; i++) {
			System.out.println("tableArray Headers["+i+"]"+Headers[i]);	
		}
*/		
		Object[][] retObjArr=null;
		
		Object[][] DData =null;
		DData = new Object [ HeadData.size() ] [ ] ;
		
		for(int i=0;i<HeadData.size();i++)
		{
			DData[ i ] = new Object[Headers.length];
			for(int j=0;j<Headers.length;j++)
			{
/*				System.out.println("HeadData.get(i).get(Headers[j])"+HeadData.get(i).get(Headers[j]));
*/				DData[i][j]=HeadData.get(i).get(Headers[j]);
/*				System.out.println("Data["+i+"]["+j+"]"+DData[i][j]);
*/			}
		}
		
		ArrayList <Object> HD = new ArrayList<Object>();
		HD.add(Headers);
		HD.add(DData);
		
		String [] a= (String[]) HD.get(0);
/*		System.out.println("a[0]"+ a[0]);
*/		
		
/*		System.out.println(Headers.toString());
		System.out.println(DData);
*/		
		return (HD);
	}
}
